﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CafeOnline.Common;
using CafeOnline.Domain.Models;
using CafeOnline.Models;

namespace CafeOnline.Controllers.Filters {
    public class CafeOnlineAuthorizedAttribute : FilterAttribute, IAuthorizationFilter {
        /// <summary>
        /// The tracer used for logging.
        /// </summary>

        #region IFields

        #endregion IFields
        #region Methods
        private UserRight? m_rights;
        private string m_operation;
        public CafeOnlineAuthorizedAttribute() {

        }

        public CafeOnlineAuthorizedAttribute(string operation, UserRight rights) {
            m_operation = operation;
            m_rights = rights;
        }

        public CafeOnlineAuthorizedAttribute(UserRight rights) {
            m_operation = string.Empty;
            m_rights = rights;
        }

        [HandleError]
        public void OnAuthorization(AuthorizationContext filterContext) {

            IUserModel user = UserModel.CurentUser;
            if (user == null || !user.IsAuthenticated) {
                filterContext.Result = new RedirectResult("~/Login", false);
            }

        }

        #endregion Methods
    }
}